package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.pickerview.TimePickerView;
import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyFestivalDuty1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.FestivalDutyBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DateUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

import static patrol.mobile.com.mobilepatrol.utils.DateUtil.format5;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyFestivalDutyActivity extends CommonListActivity implements CallBack<DateBean> {


    private View itemTopView;
    private MySpinner spOrgName;
    private MySpinner spYear;
    private ImageView ivSearch;
    private List<FestivalDutyBean> protectCollectionBeanList = new ArrayList<>();

    private String orgId = "";
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyFestivalDuty1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_FESTIVAL_DUTY_1;
    }

    @Override
    public void initView() {
        super.initView();

        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_festival_duty_list_top, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spYear = itemTopView.findViewById(R.id.spYear);
        ivSearch = itemTopView.findViewById(R.id.ivSearch);
        flContent.addView(itemTopView);

        spOrgName.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        spYear.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
    }


    @Override
    public void configAdapter() {
        super.configAdapter();
//        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                startActivity(EmergencyFestivalDuty2Activity.class);
//            }
//        });
        getOrg();
    }

    @Override
    public void setListener() {
        super.setListener();
        spYear.getmText().setText(sdf.format(new Date()));
        TimePickerView pv = new TimePickerView.Builder(EmergencyFestivalDutyActivity.this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                spYear.getmText().setText(DateUtil.convert2String(date, format5));
                getData();
            }
        }).setType(new boolean[]{true, false, false, false, false, false}).setLabel("年", "", "", null, null, null).build();
        spYear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //时间选择器
                pv.show();
//                DatePickerUtil.getInstance(EmergencyFestivalDutyActivity.this).setCallBack(EmergencyFestivalDutyActivity.this);
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String orgNme = spOrgName.getmText().getText().toString();
                String date = spYear.getmText().getText().toString();

                for (int i = 0; i < orgNameList.size(); i++) {
                    if (orgNme.equals(orgNameList.get(i))) {
                        orgId = orgIdList.get(i);
                        requestData(orgId, date);
                        return;
                    }
                }

            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                orgNameList = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }

                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                }

                getData();
                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        getData();
                    }
                });
            }
        });
    }

    private void getData() {
        String orgNme = spOrgName.getmText().getText().toString();
        String date = spYear.getmText().getText().toString();

        for (int i = 0; i < orgNameList.size(); i++) {
            if (orgNme.equals(orgNameList.get(i))) {
                orgId = orgIdList.get(i);
                requestData(orgId, date);
                return;
            }
        }
    }

    private void requestData(String orgId, String years) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_FESTIVAL_DUTY_1);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("years", years);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                protectCollectionBeanList = GsonUtil.jsonToList(listArray.toString(), FestivalDutyBean.class);
                mAdapter.updateItems(protectCollectionBeanList);
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        spYear.setText(dateBean.getmYear() + "");
        getData();
    }

}
